/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/24
 * Time: 06:16
 */
$(function(){
    var authorization = Cookies.get('_token');
    var params = {
        id: id
    };
    $.ajax({
        type: 'get',
        url: '/v1/manager/get/category.json',
        headers: {
            Authorization: authorization,
            Accept: "application/json; charset=utf-8"
        },
        data: {params: params},
        success: function(e){
            var data = JSON.parse(e);
            if (data.status == 200) {
                var json = data.body.data;
                if (json) {
                    $('.category-id').val(json.id);
                    $('.category-name').val(json.name);
                }
            }
        }
    });
    $('.btn-save').on('click', function(){
        var params = {
            id: $('.category-id').val(),
            name: $('.category-name').val()
        };
        if (checkForm(params)) {
            $.ajax({
                type: 'post',
                url: '/v1/manager/updateCategory',
                headers: {
                    Authorization: authorization,
                    Accept: "application/json; charset=utf-8"
                },
                data: {params: params},
                success: function(e){
                    var data = JSON.parse(e);
                    if (data.status == 200) {
                        alert('更新成功!');
                        location.href = '/manager/category.html';
                    }
                }
            });
        }
    });
});

function checkForm(params) {
    if (params.name == '') {
        alert('请输入分类名称');
        return false;
    }
    return true;
}